package com.question_barrier.service.impl;

import com.question_barrier.service.QuestionBankService;
import com.question_barrier.vo.question.Answers;
import com.question_barrier.vo.question.QuestionDetail;
import com.question_barrier.vo.question.TagsDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class QuestionBankServiceImpl implements QuestionBankService {
    @Autowired
    private RestTemplate restTemplate;

    public TagsDetail getAllTags(String getAllTagsUrl){
        ResponseEntity<TagsDetail> entity = restTemplate.getForEntity(getAllTagsUrl, TagsDetail.class);
        //ResponseEntity<QuestionDetail[]> entity = restTemplate.getForEntity("http://120.79.248.126/api/question/get-questions?questionCount={questionCount}&tagIdLists={tagIdLists}", QuestionDetail[].class, map);
        return entity.getBody();
    }

    public QuestionDetail getQuestion(String questionCount, List<String> tagIdLists,String getQuestionUrl){
        Map<String,String> map = new HashMap<String , String>();
        map.put("questionCount", questionCount);
        map.put("tagIdLists0",tagIdLists.get(0));
        map.put("tagIdLists1",tagIdLists.get(1));
        //map.put("tagIdLists2",tagIdLists.get(2));
        //map.put("tagIdLists3",tagIdLists.get(3));
        //ResponseEntity<QuestionDetail> entity = restTemplate.getForEntity("http://120.79.248.126/api/question/get-questions?questionCount={questionCount}&tagIdLists={tagIdLists0}&tagIdLists={tagIdLists1}&tagIdLists={tagIdLists2}&tagIdLists={tagIdLists3}", QuestionDetail.class, map);
        ResponseEntity<QuestionDetail> entity = restTemplate.getForEntity(getQuestionUrl, QuestionDetail.class, map);
        return entity.getBody();
    }

    public Answers getAnswers(String topicId,String getAnswerUrl){
        Map<String,String> map = new HashMap<String , String>();
        map.put("topicId", topicId);
        ResponseEntity<Answers> entity = restTemplate.getForEntity(getAnswerUrl, Answers.class, map);
        return entity.getBody();
    }
}
